<!doctype html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <link rel="icon" type="image/svg+xml" id="favicon" href="/logo.png" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>PandaWiki</title>
  <script>
    (function() {
      function getBasename(pathname) {
        // 路由按特殊性从高到低排序（最具体的在前）
        var routes = [
          '/doc/editor/history/:id',
          '/doc/editor/space',
          '/doc/editor/:id',
          '/doc/editor',
          '/feedback/:tab?',
          '/401',
          '/contribution',
          '/conversation',
          '/login',
          '/release',
          '/setting',
          '/stat',
        ];

        // 将路由转为正则：静态段直接匹配，:param 匹配任意段，:param? 可选
        function toRegex(route) {
          var pattern = route.split('/').filter(Boolean).map(function(s) {
            if (s.startsWith(':')) return s.endsWith('?') ? '(?:/[^/]+)?' : '/[^/]+';
            return '/' + s;
          }).join('');
          return new RegExp('^(.*?)' + pattern + '/?$');
        }

        for (var i = 0; i < routes.length; i++) {
          var match = pathname.match(toRegex(routes[i]));
          if (match) return match[1].replace(/\/$/, '');
        }

        // 未匹配：移除最后一段作为 basename
        var parts = pathname.split('/').filter(Boolean);
        if (parts.length > 1) return '/' + parts.slice(0, -1).join('/');
        if (parts.length === 1 && pathname !== '/') return pathname.replace(/\/$/, '');
        return '';
      }
      var basename = getBasename(location.pathname);
      window.__BASENAME__ = basename;
      var favicon = document.getElementById('favicon');
      if (favicon) favicon.href = (basename || '') + '/logo.png';
    })();
  </script>
</head>

<body>
  <div id="root"></div>
  <script type="module" src="/src/main.tsx"></script>
</body>

</html>